if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    $("#chart_wrap").show();
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('echarts_wrap'));
    $("#chart_wrap").hide();
    var houtai = {
        linshi_search: {
            class1: "",
            class2: "",
            search: "",
            start_time:"",
            end_time:""
        },
        receipt:{
            start_time:"",
            end_time:"",
            state:"",
            search:"",
            id:"",
            state_q:""
        },
        water:{
            start_time:"",
            end_time:"",
            state:""
        },
        chart:{
            start_time:"",
            end_time:"",
            type:""
        },
        all_chart:false,
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        linshi_arrid: [],
        linshi_type: null, // 1修改发票状态
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 导出excel
        excel:(function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html  xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); },
                format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); };
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById("tableExcel");
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
               if($("#dlink").length<1) $("body").append($("<a id='dlink' style='display: none'></a>"));

                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name+'.xls';
                document.getElementById("dlink").click();
            };
        })(),
        // 获取商品的数组
        shop_list_ajax: function (page_number) {
            var n = page_number===undefined ?1  : page_number;
            var ispring=n!==0;
            $.ajax({
                url: ip + 'index.php?s=admincms/fiscal/show',
                type: "post",
                async:ispring,
                data: {
                    pid: houtai.linshi_search.class1,
                    cid: houtai.linshi_search.class2,
                    start_time:houtai.linshi_search.start_time,
                    end_time:houtai.linshi_search.end_time,
                    page: n,
                    serach: houtai.linshi_search.search,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.table_string(res);
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            // 写入table
            var string_head = "<thead> <tr> <th>编号</th> <th>名称</th> <th>品牌</th> <th>规格</th> <th>颜色</th><th>销量</th><th>盈利</th> <th>操作</th> </tr> </thead>";
            var string_body = "";
           if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="caiwu_id" >' + o.pid + '</td>' +
                        '<td class="caiwu_name">' + o.names + '</td>' +
                        '<td class="caiwu_brand">' + o.brand + '</td>' +
                        '<td class="caiwu_specs">' + o.specs + '</td>' +
                        '<td class="caiwu_color">' + o.color + '</td>' +
                        '<td class="caiwu_salesNumber">'+o.sales+'</td>' +
                        '<td class="caiwu_gain">'+o.profit+'</td>' +
                        '<td style="width:100px;"><button class="btn btn-default chart_btn">图表分析</button></td> ' +
                        '</tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            }

                $("#caiwu_table").empty().append(string_head + string_body);
        },
        //发票列表ajax
        receipt_list_ajax:function (page_number) {
            var n = page_number===undefined ?1  : page_number;
            $.ajax({
                url: ip + 'index.php?s=admincms/invoice/showList',
                type: "post",
                data: {
                    startTime:houtai.receipt.start_time,
                    endTime:houtai.receipt.end_time,
                    status:houtai.receipt.state,
                    page: n,
                    search: houtai.receipt.search,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.receipt_string(res);
                    $("#page2").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.receipt_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的发票数组写入table
        receipt_string: function (res) {
            // 写入table
            var string_head = "<thead> <tr> <th>订单编号</th><th>发票信息</th> <th>收票人信息</th> <th>时间</th><th>操作</th></tr> </thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    // 如果有发票信息
                    var  receipt_string="";
                    var receipt_btn="";
                    var receipt_user="";
                    if(o.invoiceList){
                        if(o.invoiceList.invoice){
                              receipt_string= '<p><span>公司名称：</span> <b>' + o.invoiceList.invoice.companyName + '</b></p>' +
                                '<p><span>纳税编号：</span> <b>' + o.invoiceList.invoice.taxpayerCode + '</b></p>' +
                                '<p><span>公司地址：</span> <b>' + o.invoiceList.invoice.regAddr + '</b></p>' +
                                '<p><span>公司电话：</span> <b>' + o.invoiceList.invoice.regPhone + '</b></p>' +
                                '<p><span>公司银行：</span> <b>' + o.invoiceList.invoice.banks + '</b></p>'+
                                '<p><span>公司账户：</span> <b>' + o.invoiceList.invoice.banksAccount + '</b></p>';
                             receipt_btn=o.invoiceList.invoice.status===0?'<button class="btn btn-success complete_btn btn-xs">完成</button>':'<p style="color:#5cb85c;font-weight: 900;">已完成</p>';
                        }
                        if(o.invoiceList.addr){
                               receipt_user= '<p><span>收票人：</span> <b>' + o.invoiceList.addr.names + '</b></p>' +
                                '<p><span>电话：</span> <b>' + o.invoiceList.addr.phone + '</b></p>' +
                                '<p><span>收票地址：</span> <b>' + o.invoiceList.addr.region+ '</b></p>' +
                                '<p><span>详细地址：</span> <b>' + o.invoiceList.addr.address + '</b></p>';
                        }
                    }
                    string_body += '<tr>' +
                        '<td class="receipt_id" style="display: none;" >' + o.id + '</td>' +
                        '<td class="order_id"><p>' + o.orderid + '</p></td>' +
                        '<td class="receipt_info">' + receipt_string+ '</td>' +
                        '<td class="receipt_user">' +receipt_user + '</td>' +
                        '<td class="receipt_time">'+o.createTime+'</td>' +
                        '<td style="width:100px;" class="receipt_control">'+receipt_btn+'</td> ' +
                        '</tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            }

            $("#receipt_table").empty().append(string_head + string_body);
        },
        //修改发票状态
        set_invoice_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/invoice/invoiceStatus",
                type: "post",
                data: {
                    status: houtai.receipt.state_q,
                    orderId: houtai.linshi_id,
                    invoiceId:houtai.receipt.id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                     $.each($("#receipt_table .order_id>p"),function (i,o) {
                         if(houtai.linshi_id===$(this).text()){
                            $(this).parents("tr").find(".receipt_control").empty().append('<p style="color:#5cb85c;font-weight: 900;">已完成</p>');
                             houtai.hint("<span style='color:#0f0;'>修改成功！！</span>");
                             $("#modal_wrap").fadeOut();
                         }
                     });
                    }
                },
                error:function () {
                    houtai.hint("请求失败!!!");
                }
            });
        },
        //初始类
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/product",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                async: false,
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.arr = res.data;
                        for (var ii = 0, string11 = '<option value="">全部</option>'; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                        }
                        $("#select1").empty().append(string11);
                        $("#select2").empty().append('<option value="">全部</option>');
                    }
                },
                error:function () {
                   houtai.hint("初始类失败！！");
                }
            });
        },
        //所有商品数据分析请求
        chart_all_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/fiscal/allShowItem',
                type: "post",
                data: {
                    start_time:houtai.chart.start_time,
                    end_time:houtai.chart.end_time,
                    order:houtai.chart.type,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    var isMonth=(houtai.chart.end_time-houtai.chart.start_time)>2678400?"":$("#select_month option:selected").text()+"月";
                    var unit=$("#select_type").val()==="0"?"件":"元";
                    for(var i=0,times=[],data_=[];i<res.data.length;i++){
                        times.push(res.data[i].times);
                        data_.push(res.data[i].sales===null?0:res.data[i].sales);
                    }
                    if(res.resStatus === 0){
                        var option = {
                            title: {
                                text: "所有商品",
                                subtext: $("#select_year option:selected").text()+'年'+isMonth+$("#select_type option:selected").text()+'分析图'
                            },
                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                data:[$("#select_type option:selected").text()]
                            },
                            toolbox: {
                                show: true,
                                feature: {
                                    dataZoom: {
                                        yAxisIndex: 'none'
                                    },
                                    dataView: {readOnly: false},
                                    magicType: {type: ['line', 'bar']},
                                    restore: {},
                                    saveAsImage: {}
                                }
                            },
                            xAxis:  {
                                type: 'category',
                                boundaryGap: false,
                                data:times
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value} '+unit
                                }
                            },
                            series: [
                                {
                                    name:$("#select_type option:selected").text(),
                                    type:'line',
                                    data:data_,
                                    markPoint: {
                                        data: [
                                            {type: 'max', name: '最大值'},
                                            {type: 'min', name: '最小值'}
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            {type: 'average', name: '平均值'}
                                        ]
                                    }
                                }
                            ]
                        };
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    }
                }

            });
        },
        //单个商品数据分析请求
        chart_one_ajax:function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/fiscal/showItem',
                type: "post",
                data: {
                    pid:houtai.linshi_id,
                    start_time:houtai.chart.start_time,
                    end_time:houtai.chart.end_time,
                    order:houtai.chart.type,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    var name_="";
                    for(var a=0;a<$("#caiwu_table .caiwu_id").length;a++){
                        if(houtai.linshi_id==$("#caiwu_table .caiwu_id").eq(a).text()){
                          name_=$("#caiwu_table .caiwu_id").eq(a).siblings(".caiwu_name").text();
                            break;
                        }
                    }
                    var isMonth=(houtai.chart.end_time-houtai.chart.start_time)>2678400?"":$("#select_month option:selected").text()+"月";
                    var unit=$("#select_type").val()==="0"?"件":"元";
                    for(var i=0,times=[],data_=[];i<res.data.length;i++){
                        times.push(res.data[i].times);
                        data_.push(res.data[i].sales===null?0:res.data[i].sales);
                    }
                    if(res.resStatus === 0){
                        var option = {
                            title: {
                                text: name_,
                                subtext: $("#select_year option:selected").text()+'年'+isMonth+$("#select_type option:selected").text()+'分析图'
                            },
                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                data:[$("#select_type option:selected").text()]
                            },
                            toolbox: {
                                show: true,
                                feature: {
                                    dataZoom: {
                                        yAxisIndex: 'none'
                                    },
                                    dataView: {readOnly: false},
                                    magicType: {type: ['line', 'bar']},
                                    restore: {},
                                    saveAsImage: {}
                                }
                            },
                            xAxis:  {
                                type: 'category',
                                boundaryGap: false,
                                data:times
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value} '+unit
                                }
                            },
                            series: [
                                {
                                    name:$("#select_type option:selected").text(),
                                    type:'line',
                                    data:data_,
                                    markPoint: {
                                        data: [
                                            {type: 'max', name: '最大值'},
                                            {type: 'min', name: '最小值'}
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            {type: 'average', name: '平均值'}
                                        ]
                                    }
                                }
                            ]
                        };
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    }
                }

            });
        },
        //交易流水请求
        water_ajax:function (page_number) {
            var n = page_number===undefined ?1  : page_number;
            var ispring=n!==0;
            $.ajax({
                url: ip + 'index.php?s=admincms/orders/tradingFlow',
                type: "post",
                async:ispring,
                data: {
                    start_time:houtai.water.start_time,
                    end_time:houtai.water.end_time,
                    page :n,
                    tp:houtai.water.state,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.water_string(res);
                    $("#page1").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.water_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        water_string:function (res) {
            // 写入table
            var string_head = "<thead> <tr> <th>订单号</th> <th>金额</th> <th>类型</th> <th>时间</th> <th>用户名</th></tr> </thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="caiwu_id" style="mso-number-format:\'\\@\';">' + o.orderid  + '</td>' +
                        '<td class="caiwu_name">' + o.price + '</td>' +
                        '<td class="caiwu_brand">' + o.tp + '</td>' +
                        '<td class="caiwu_specs">' + o.pay_time  + '</td>' +
                        '<td class="caiwu_color">' + o.user  + '</td>' +
                        '</tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            }

            $("#add_taocan_table").empty().append(string_head + string_body);
        },
        init:function () {
            this.chushi_class();
            this.linshi_search.class1 = $("#select1").val();
            this.linshi_search.class2 = $("#select2").val();
            this.shop_list_ajax();

            var lishiid=location.search.match(/\d+/)&&location.search.match(/\d+/)[0];
            if(lishiid){
                houtai.receipt.search=lishiid;
                $("#receipt_wrap").fadeIn();
                houtai.receipt_list_ajax();
            }
            // 按时间写入需要查询的年月
            $("#select_year").val(new Date().getFullYear()+"");
            $("#select_month").val(new Date().getMonth()+1+"");
            // 根据年月和类型写入搜索条件
            this.chart.start_time=new Date($("#select_year").val()+"-"+$("#select_month").val()+"-01 00:00")/1000;
            var end_time_year=$("#select_year").val();
            var end_time_month=Number($("#select_month").val())+1;
            // 如果是12月，用12+1找不到，所以当等于13时月份变为1，年变为当前年+1
            if(end_time_month===13){
                end_time_month=1;
                end_time_year=Number(end_time_year)+1;
            }
            houtai.chart.end_time=new Date(end_time_year+"-"+end_time_month+"-01 00:00")/1000;
            this.chart.type=$("#select_type").val();
        }
    };

    houtai.init();
    //时间插件
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    //时间插件
    $('#input_time1').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    //发票时间插件
    $('#input_time2').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });

    // 一级下拉事件
    $("#select1").bind("change", function () {
        if($(this).val()===""){
            $("#select2").empty().append('<option value="">全部</option>');
        }else{
            for (var b = 0; b < houtai.arr.length; b++) { //循环分类查找对应的子集
                if (houtai.arr[b].pid ==$(this).val()) {
                    break;
                }
            }
            for (var a = 0, string2 = '<option value="">全部</option>'; a < houtai.arr[b].activity_list.length; a++) {
                string2 += '<option value="' + houtai.arr[b].activity_list[a].cid + '">' + houtai.arr[b].activity_list[a].classnames + '</option>';
            }
            $("#select2").empty().append(string2);
        }


        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.search = "";
        houtai.linshi_search.start_time = "";
        houtai.linshi_search.end_time = "";
        houtai.shop_list_ajax();
    });
    // 二级下拉事件
    $("#select2").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.search = "";
        houtai.linshi_search.start_time = "";
        houtai.linshi_search.end_time = "";
        houtai.shop_list_ajax();
    });
    //点击日期的小图标跳出选择日期
    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    // 按时间搜索
    $("#search_time").bind("click", function () {
        if ($.trim($("#input_time").val()) === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_search.search ="";
                houtai.linshi_search.start_time = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_search.end_time= new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.shop_list_ajax();
            }
        }

    });
    //按名称搜索
    $("#search_btn").bind("click", function () {
        if ($("#search").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.class1="";
            houtai.linshi_search.class2="";
            houtai.linshi_search.start_time="";
            houtai.linshi_search.end_time="";
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.shop_list_ajax();
        }
    });
    //回车按钮操作
    $("#search_kucun_form").submit(function () {
        if ($("#search").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.class1="";
            houtai.linshi_search.class2="";
            houtai.linshi_search.start_time="";
            houtai.linshi_search.end_time="";
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.shop_list_ajax();
        }
        return false;
    });
    //导出excel
     $("#excel").bind("click",function () {
         if($("#caiwu_table tr:last>td").length>1){
             houtai.shop_list_ajax(0);
             var title=houtai.linshi_search.start_time?($("#select1>option:selected").text()+"-"+$("#select2>option:selected").text()+"-"+$("#input_time").val()):($("#select1>option:selected").text()+"-"+$("#select2>option:selected").text());
             houtai.excel($("#caiwu_table")[0],title);
         }else{
             houtai.hint("没有数据怎么导出啊？");
         }
     });
  // 流水记录操作
    $("#trade_water").bind("click",function () {
          houtai.water_ajax();
       $("#water_record").show();
    });
    //发票管理
    $("#receipt_btn").bind("click",function () {
        houtai.receipt.state=$("#receipt_type").val();
        houtai.receipt.start_time="";
        houtai.receipt.end_time="";
        houtai.receipt.search="";
        houtai.receipt_list_ajax();
        $("#receipt_wrap").show();
    });
    // 导出excel
    $("#record_all_btn").bind("click",function () {
        if($("#add_taocan_table tr:last>td").length>1){
            houtai.water_ajax(0);
            var title=houtai.water.start_time?("交易流水-"+$("#water_type>option:selected").text()+"-"+$("#input_time1").val()):("交易流水-"+$("#water_type>option:selected").text());
            houtai.excel($("#add_taocan_table")[0],title);
        }else{
            houtai.hint("没有数据怎么导出啊?");
        }

    });
    // 按时间搜索流水
    $("#search_time1").bind("click", function () {
        if ($.trim($("#input_time1").val()) === "") {
            $("#input_time1").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.water.start_time = new Date($("#input_time1").val().match(regexp)[1]).getTime() / 1000;
                houtai.water.end_time= new Date($("#input_time1").val().match(regexp)[2]).getTime() / 1000;
                houtai.water_ajax();
            }
        }

    });
   //按类型搜索
    $("#water_type").bind("change",function () {
       houtai.water.state=$(this).val();
       houtai.water.start_time="";
       houtai.water.end_time="";
        houtai.water_ajax();
    });

  /*  图表*/
    //总图表分析按钮
    $("#chart_all_btn").bind("click",function () {
        houtai.all_chart=true;
        $("#chart_wrap").show();
        houtai.chart_all_ajax();
    });
    //单个商品的图表分析按钮
    $("#caiwu_table").on("click",".chart_btn",function () {
        houtai.linshi_id=$(this).parents("tr").find(".caiwu_id").text();
        houtai.all_chart=false;
        $("#chart_wrap").show();
        houtai.chart_one_ajax();
    });
    //年搜索下拉
    $("#select_year").bind("change",function () {
            houtai.chart.start_time=new Date($("#select_year").val()+"-01-01 00:00")/1000;
            houtai.chart.end_time=new Date((Number($("#select_year").val())+1)+"-01-01 00:00")/1000;
            if(houtai.all_chart){
                houtai.chart_all_ajax();
            }else{
                houtai.chart_one_ajax();
            }

    });
    //月搜索下拉
    $("#select_month").bind("change",function () {
            houtai.chart.start_time=new Date($("#select_year").val()+"-"+$("#select_month").val()+"-01 00:00")/1000;
            var end_time_year=$("#select_year").val();
            var end_time_month=Number($("#select_month").val())+1;
            // 如果是12月，用12+1找不到，所以当等于13时月份变为1，年变为当前年+1
            if(end_time_month===13){
                end_time_month=1;
                end_time_year=Number(end_time_year)+1;
            }
            houtai.chart.end_time=new Date(end_time_year+"-"+end_time_month+"-01 00:00")/1000;
        if(houtai.all_chart){
            houtai.chart_all_ajax();
        }else{
            houtai.chart_one_ajax();
        }
    });
    //类型搜索下拉
    $("#select_type").bind("change",function () {
        houtai.chart.type=$(this).val();
        if(houtai.all_chart){
            houtai.chart_all_ajax();
        }else{
            houtai.chart_one_ajax();
        }
    });
    window.onresize = function () {
        myChart.resize();
    };

    /*发票*/
    // 点击订单号跳转
    $("#receipt_table").on("click",".order_id>p",function () {
        location.href="order_admin.html?"+$(this).text();
    });
    //点击修改发票状态
    $("#receipt_table").on("click",".complete_btn",function () {
       $("#modal_wrap").fadeIn();
       $(".modal-body").empty().append("你确定要把发票状态改为<span style='color:#f10;'>已完成</span>吗？");
       houtai.linshi_type=1;
       houtai.linshi_id=$(this).parents("tr").find(".order_id>p").text();
       houtai.receipt.id=$(this).parents("tr").find(".receipt_id").text();
        houtai.receipt.search="";
       houtai.receipt.state_q=1;
    });
    // 按类型搜索
  $("#receipt_type").bind("change",function () {
   houtai.receipt.state=$(this).val();
   houtai.receipt.start_time="";
   houtai.receipt.end_time="";
   houtai.receipt.search="";
   houtai.receipt_list_ajax();
  });
  // 按时间搜索
  $("#search_time2").bind("click",function () {
   houtai.receipt.state=$(this).val();
      // 匹配的正则表达式
      var regexp = /(\d+-\d+-[^-]+)-(.+)/;
      houtai.receipt.start_time = new Date($("#input_time2").val().match(regexp)[1]).getTime() / 1000;
      houtai.receipt.end_time= new Date($("#input_time2").val().match(regexp)[2]).getTime() / 1000;
      houtai.receipt.search="";
   houtai.receipt_list_ajax();
  });
  // 按内容搜索
  $("#search_all").bind("click",function () {
      if($.trim($("#search_input").val())===""){
          houtai.hint("搜索不能为空！！");
          $("#search_input").focus();
      }else{
          houtai.receipt.state=$(this).val();
          houtai.receipt.start_time ="";
          houtai.receipt.end_time= "";
          houtai.receipt.search=$.trim($("#search_input").val());
          houtai.receipt_list_ajax();
      }

  });

    /*取消操作*/
    // 修改取消
    $(".close").bind("click", function () {
        $(".set_shangpin").fadeOut(200);
    });
    // 点击取消确认框
    $("#modal_close").bind("click",function () {
       $("#modal_wrap").fadeOut();
    });
    // 点击确认框
    $("#confirm").bind("click",function () {
        // 如果为1修改发票状态
       if(houtai.linshi_type===1){
           if(houtai.click_yes()){
               houtai.set_invoice_ajax();
           }
       }
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    //点任何地方收回退出按钮
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
});